package com.xmall.utils;

import cn.hutool.core.date.DateUtil;
import com.xmall.define.constants.CommonConstants;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.Duration;
import java.util.Date;

@Component
public class RedisUtils {

    private static RedisUtils redisUtils;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostConstruct
    public void init() {
        redisUtils = this;
        redisUtils.redisTemplate = this.redisTemplate;
    }

    public static Boolean setTimerTaskFlag(String taskKey) {
        return redisUtils.redisTemplate.opsForValue().setIfAbsent(CommonConstants.TimerTask.REDIS_KEY_PREFIX + taskKey, DateUtil.formatDateTime(new Date()), Duration.ofMinutes(CommonConstants.TimerTask.EXPIRE_MINUTES));
    }

    public static void deleteTimerTaskFlag(String taskKey) {
        redisUtils.redisTemplate.delete(CommonConstants.TimerTask.REDIS_KEY_PREFIX + taskKey);
    }
}

